package com.hao1st.service1.biz.rpcdemo.controller;

import com.hao1st.service1.rpc.Service2Rpc;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rpc")
public class RpcController {

    @Resource
    private Service2Rpc service2Rpc;

    /**
     * 熔断工厂类
     */
    @Resource
    private CircuitBreakerFactory cbFactory;

    @GetMapping("/invoke")
    public String rpcService2(@RequestHeader HttpHeaders httpHeaders) {

        // 调用接口访问service2
        return cbFactory.create("rpcService2").run(() -> service2Rpc.rpcService2(httpHeaders), throwable -> "service2的服务不可用");
    }
}
